#include <gtk/gtk.h>
#include <panel-applet.h>
#include <libnotify/notify.h>
#include "bitly.h"
#include "config.h"

GtkWidget* frm_preferences;
GtkWidget* frm_main;
GtkWidget* txt_login;
GtkWidget* txt_api_key;
GtkWidget* txt_long_url;
GtkWidget* txt_short_url;
GtkWidget* chk_long_url;
PanelApplet *panel_applet;
NotifyNotification *notification;
guint g_timeout_id = 0;
char *last_url = NULL;
GtkClipboard* cboard;

gboolean show_long_url(gpointer data)
{
	char* text = gtk_clipboard_wait_for_text(cboard);
	if(text==NULL || strstr(text,"http://bit.ly")!=text || strcmp(text,last_url)==0)
	{	
		return TRUE;
	}

	gchar *login = (gchar *)panel_applet_gconf_get_string(PANEL_APPLET(panel_applet),"bitly_login",NULL);
	gchar *api_key = (gchar *)panel_applet_gconf_get_string(PANEL_APPLET(panel_applet),"bitly_api_key",NULL);

	if(login!=NULL && api_key!=NULL && strlen(login)>0 && strlen(api_key)>0)
	{
		notify_notification_update(notification,text,expand_link(text,login,api_key),DATA_DIR "/gbitly-applet/bitly.png");
		notify_notification_show(notification,NULL);
		last_url = (char *)realloc((void *)last_url,strlen(text)+1);
		strcpy(last_url,text);
		return TRUE;
	}
	else
	{
		GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(frm_main),GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK,"You need to set preferences first");
		gtk_dialog_run(GTK_DIALOG(dialog));
		gtk_widget_destroy(dialog);
		g_timeout_id=0;
		return FALSE;
	}
	return TRUE;
}

gboolean show_menu(GtkWidget* box,GdkEventButton* event, gpointer data)
{
	if(event->button==1)
	{
		gtk_window_set_keep_above(GTK_WINDOW(frm_main),TRUE);
		gint height;
		gint width;


		char* text = gtk_clipboard_wait_for_text(cboard);
		if(text!=NULL && (strstr(text,"http://")==text || strstr(text,"ftp://")==text || strstr(text,"https://")==text))
		{
			gtk_entry_set_text(GTK_ENTRY(txt_long_url),text);
			gtk_entry_set_text(GTK_ENTRY(txt_short_url),"");
		}
		

		gtk_widget_show_all(GTK_WIDGET(frm_main));
		gtk_window_get_size(GTK_WINDOW(frm_main),&width,&height);

		int x=0,y=0;
		gdk_window_get_origin(box->window,&x,&y);	

		switch(panel_applet_get_orient(PANEL_APPLET(data)))
		{			
			case PANEL_APPLET_ORIENT_DOWN:
				gtk_window_move(GTK_WINDOW(frm_main),x,y + (panel_applet_get_size(PANEL_APPLET(data))));
				break;
			case PANEL_APPLET_ORIENT_UP:
				gtk_window_move(GTK_WINDOW(frm_main),x,y - height);
				break;
			case PANEL_APPLET_ORIENT_LEFT:
				gtk_window_move(GTK_WINDOW(frm_main),x - width,y);
				break;
			case PANEL_APPLET_ORIENT_RIGHT:
				gtk_window_move(GTK_WINDOW(frm_main),x + panel_applet_get_size(PANEL_APPLET(data)),y);
				break;
		}	
		return TRUE;
	}
	return FALSE;
}

void make_short(GtkWidget* box, gpointer data)
{
	gchar *login = (gchar *)panel_applet_gconf_get_string(PANEL_APPLET(panel_applet),"bitly_login",NULL);
	gchar *api_key = (gchar *)panel_applet_gconf_get_string(PANEL_APPLET(panel_applet),"bitly_api_key",NULL);

	if(login!=NULL && api_key!=NULL && strlen(login)>0 && strlen(api_key)>0)
	{
		char *text = shorten_link(gtk_entry_get_text(GTK_ENTRY(txt_long_url)),login,api_key);
		gtk_entry_set_text(GTK_ENTRY(txt_short_url),text);
		last_url = (char *)realloc((void *)last_url,strlen(text)+1);
		strcpy(last_url,text);
	}
	else
	{
		GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(frm_main),GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK,"You need to set preferences first");
		gtk_dialog_run(GTK_DIALOG(dialog));
		gtk_widget_destroy(dialog);
	}
}

void but_close_clicked(GtkWidget* box, gpointer data)
{
	gtk_widget_hide_all(GTK_WIDGET(frm_main));
}

void show_preference(BonoboUIComponent *component,gpointer user_data,const char *cname)
{
	gtk_entry_set_text(GTK_ENTRY(txt_login),(gchar *)panel_applet_gconf_get_string(PANEL_APPLET(user_data),"bitly_login",NULL));
	gtk_entry_set_text(GTK_ENTRY(txt_api_key),(gchar *)panel_applet_gconf_get_string(PANEL_APPLET(user_data),"bitly_api_key",NULL));
	
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chk_long_url),panel_applet_gconf_get_bool(PANEL_APPLET(user_data),"bitly_long_url",NULL));

	gint response = gtk_dialog_run(GTK_DIALOG(frm_preferences));
	gtk_widget_hide(GTK_WIDGET(frm_preferences));
	if(response==1)
	{
		panel_applet_gconf_set_string(PANEL_APPLET(user_data),"bitly_login",gtk_entry_get_text(GTK_ENTRY(txt_login)),NULL);
		panel_applet_gconf_set_string(PANEL_APPLET(user_data),"bitly_api_key",gtk_entry_get_text(GTK_ENTRY(txt_api_key)),NULL);
		panel_applet_gconf_set_bool(PANEL_APPLET(user_data),"bitly_long_url",gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(chk_long_url)),NULL);
	}
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(chk_long_url))==TRUE)
	{
		if(g_timeout_id==0) g_timeout_id = g_timeout_add(1000,show_long_url,NULL);
	}
	else
	{
		if(g_timeout_id>0) 
		{
			g_source_remove(g_timeout_id);
			g_timeout_id = 0;
		}
	}
}

gboolean frm_preference_delete_event(GtkWidget* widget,GdkEvent* event,gpointer data)
{
	gtk_widget_hide_all(GTK_WIDGET(frm_preferences));
	return TRUE;
}

static const char menu_xml[] = "<popup name=\"button3\">"
	"<menuitem name=\"Preferences\" verb=\"Show_Preferences\" _label=\"Preferences\" />"
	"</popup>";

static const BonoboUIVerb menu_verb[] = {	BONOBO_UI_VERB("Show_Preferences", show_preference),
		BONOBO_UI_VERB_END
	};


static gboolean make_applet(PanelApplet *applet,const char *giid, gpointer data)
{
	notify_init("gBitLyApplet");
	last_url = (char *)malloc(1);
	if(strcmp(giid,"OAFIID:gBitLyApplet")!=0) return FALSE;

	panel_applet = applet;
	
	GtkWidget *eventbox = gtk_event_box_new();
	GtkWidget *image = gtk_image_new_from_file(DATA_DIR "/gbitly-applet/bitly.png");
	notification = notify_notification_new("Welcome","Welcome",DATA_DIR "/gbitly-applet/bitly.png",NULL);
	notify_notification_set_timeout(notification,NOTIFY_EXPIRES_DEFAULT);

	gtk_container_add(GTK_CONTAINER(eventbox), image);
	gtk_container_add(GTK_CONTAINER(applet),eventbox);
	cboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);

	g_signal_connect(G_OBJECT(eventbox),"button_press_event",G_CALLBACK(show_menu),applet);

	GtkBuilder* xml = gtk_builder_new();
	gtk_builder_add_from_file(xml,DATA_DIR "/gbitly-applet/ui.xml",NULL);
	gtk_builder_connect_signals(xml,NULL);

	frm_preferences = GTK_WIDGET(gtk_builder_get_object(xml,"frm_preferences"));
	frm_main = GTK_WIDGET(gtk_builder_get_object(xml,"frm_main"));
	txt_login = GTK_WIDGET(gtk_builder_get_object(xml,"txt_login"));
	txt_api_key = GTK_WIDGET(gtk_builder_get_object(xml,"txt_api_key"));
	txt_short_url = GTK_WIDGET(gtk_builder_get_object(xml,"txt_short_url"));
	txt_long_url = GTK_WIDGET(gtk_builder_get_object(xml,"txt_long_url"));
	chk_long_url = GTK_WIDGET(gtk_builder_get_object(xml,"chk_long_url"));

	
	gtk_widget_show_all(GTK_WIDGET(applet));

	if(panel_applet_gconf_get_bool(PANEL_APPLET(applet),"bitly_long_url",NULL)==TRUE)
	{
		g_timeout_id = g_timeout_add(1000,show_long_url,NULL);
	}
	
	panel_applet_setup_menu(PANEL_APPLET(applet),menu_xml,menu_verb,applet);

	return TRUE;
}

PANEL_APPLET_BONOBO_FACTORY("OAFIID:gBitLyApplet_Factory",PANEL_TYPE_APPLET,
		"gBitLy Applet","0",make_applet, NULL);
/*
int main(int argc, char *argv[])
{
	gtk_init(&argc,&argv);

	GtkBuilder* xml = gtk_builder_new();
	gtk_builder_add_from_file(xml,"/usr/local/share/gbitly-applet/ui.xml",NULL);
	gtk_builder_connect_signals(xml,NULL);

	frm_preferences = GTK_WIDGET(gtk_builder_get_object(xml,"frm_preferences"));
	frm_main = GTK_WIDGET(gtk_builder_get_object(xml,"frm_main"));

	gtk_main();
}
*/
